package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.time.Instant;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    //mBtAmClick  前面加了个m  代表成员变量
    private Button mBtAmClick;
    private TextView mTvAmFrom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //ctrl+shift+i 格式化代码
        setContentView(R.layout.activity_main);
        //alt+ins 自动生成
        initView();
        initData();
        String form = getIntent().getStringExtra("form");
        if ("LifeCycleActivity".equals(form)) {
            mTvAmFrom.setText("form：LifeCycleActivity");
            mTvAmFrom.setOnClickListener(this);
        }else {
            //表示刚进这个页面就跳转到LayoutActivity这样页面，并把当前页面关掉。
            startActivity(new Intent(this, LifeCycleActivity.class));
            finish();
        }
    }

    private void initData() {
        mBtAmClick.setOnClickListener(this);
    }

    private void initView() {
        mBtAmClick = (Button) findViewById(R.id.bt_am_click);
        mTvAmFrom = (TextView) findViewById(R.id.tv_am_from);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_am_click:
                Toast.makeText(MainActivity.this, "你好啊2", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_am_from:
                Intent i = new Intent();
                i.putExtra("form","MainActivity");
                setResult(RESULT_OK,i);
                finish();
                break;
        }
    }
}